﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
Imports Microsoft.Reporting.WebForms

Partial Class Reportes_ResumenPriorizacion
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            llenarCombos()
            hdStoredProcedure.Value = "RDLPriorizacionConsolidada"
            hdReporteName.Value = "rptPriorizacionFamilias.rdlc"
            cargardatos()
            'traerProyectos()
        End If


    End Sub
    Sub llenarCombos()
        Dim u As New Web_Usuario
        Using h As New Helper
            u = h.GetUsuario(Context.User.Identity.Name)
        End Using
        Using bd As New BdHogaresDataContext
            Dim a = From i In bd.Web_Proyectos
                    Where (i.web_Operadore.idOperador = u.IdOperador) Or IsNothing(u.IdOperador)
                    Select i.idProyecto, i.CodigoProyecto
            ddlProyectos.DataSource = a
            ddlProyectos.DataValueField = "CodigoProyecto"
            ddlProyectos.DataTextField = "CodigoProyecto"
            ddlProyectos.DataBind()

            If IsNothing(u.IdOperador) Then
                ddlProyectos.Items.Insert(0, New ListItem("Todos", "0"))
            End If

            cargarDeptos()
            cargarMunicipios()

            Dim b = From i In bd.Web_Parametros
                    Where (i.Categoria = "visitas")
                    Select i.NombreOpcion, i.ValorOpcion

            ddlVisita.DataSource = b
            ddlVisita.DataValueField = "NombreOpcion"
            ddlVisita.DataTextField = "NombreOpcion"
            ddlVisita.DataBind()
            ddlVisita.Items.Insert(0, New ListItem("Todas", "0"))

        End Using

        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()

            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_RPTCruceFiltro", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@campo1", "Localizacion")
                ddlLocalizacion.DataSource = cmd.ExecuteReader
                ddlLocalizacion.DataTextField = "detalle"
                ddlLocalizacion.DataValueField = "valor"
                ddlLocalizacion.DataBind()
                ddlLocalizacion.Items.Insert(0, New ListItem("Todos", "0"))
            End Using

            cnn.Close()

        End Using


        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()

            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_OpcionesListar", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@categoria", "Sexo")
                ddlSexo.DataSource = cmd.ExecuteReader
                ddlSexo.DataTextField = "NombreOpcion"
                ddlSexo.DataValueField = "valorOpcion"
                ddlSexo.DataBind()
                ddlSexo.Items.Insert(0, New ListItem("Todos", "0"))
            End Using

            cnn.Close()

        End Using

    End Sub

    Sub cargardatos()
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)

            Using da As New SqlDataAdapter

                Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(hdStoredProcedure.Value, cnn)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@codProyecto", ddlProyectos.SelectedValue)
                    cmd.Parameters.AddWithValue("@codMunicipio", ddlMunicipio.SelectedValue)
                    cmd.Parameters.AddWithValue("@Visita", ddlVisita.SelectedValue)
                    cmd.Parameters.AddWithValue("@depto", ddlDepto.SelectedValue)
                    cmd.Parameters.AddWithValue("@localizacion", ddlLocalizacion.SelectedValue)
                    cmd.Parameters.AddWithValue("@Sexo", ddlSexo.SelectedValue)

                    da.SelectCommand = cmd

                    Using thisDTS As New DataSet
                        da.Fill(thisDTS)
                        Dim datasourse As New ReportDataSource("dtsHogares", thisDTS.Tables(0))
                        ReportViewer1.LocalReport.ReportPath = "Reportes\rdls\" & hdReporteName.Value

                        ReportViewer1.LocalReport.DataSources.Clear()
                        ReportViewer1.LocalReport.DataSources.Add(datasourse)
                        ReportViewer1.LocalReport.Refresh()

                        Dim datasourse2 As New ReportDataSource("dtsHogares", thisDTS.Tables(0))
                        ReportViewer2.LocalReport.ReportPath = "Reportes\rdls\rptPriorizacionFamiliasDetalle.rdlc"

                        ReportViewer2.LocalReport.DataSources.Clear()
                        ReportViewer2.LocalReport.DataSources.Add(datasourse)
                        ReportViewer2.LocalReport.Refresh()


                    End Using
                End Using

            End Using
        End Using


    End Sub

#Region "filtros"

    Sub cargarDeptos()
        Using bd As New bdReportesDataContext

            ddlDepto.DataSource = bd.RPT_DepartamentosXProyecto(ddlProyectos.SelectedValue)
            ddlDepto.DataValueField = "Valor"
            ddlDepto.DataTextField = "Detalle"
            ddlDepto.DataBind()
        End Using
    End Sub

    Sub cargarMunicipios()
        Using bd As New bdReportesDataContext

            ddlMunicipio.DataSource = bd.RPT_MunicipiosXProyectoXDepartamento(ddlProyectos.SelectedValue, ddlDepto.SelectedValue)
            ddlMunicipio.DataValueField = "Valor"
            ddlMunicipio.DataTextField = "Detalle"
            ddlMunicipio.DataBind()
        End Using
    End Sub

    Protected Sub ddlProyectos_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProyectos.SelectedIndexChanged
        cargarDeptos()
        cargarMunicipios()
    End Sub


    Protected Sub ddlDepto_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepto.SelectedIndexChanged
        cargarMunicipios()
    End Sub
#End Region
    'Sub traerProyectos() ' carga el control ddlNombreProyecto con los proyectos que han ingresado
    '    Dim u As New Web_Usuario
    '    Using h As New Helper
    '        u = h.GetUsuario(Context.User.Identity.Name)
    '    End Using
    '    Using bd As New BdHogaresDataContext
    '        Dim a = From i In bd.Web_Proyectos
    '                Where (i.web_Operadore.idOperador = u.IdOperador) Or IsNothing(u.IdOperador)
    '                Select i.idProyecto, i.CodigoProyecto
    '        ddlProyectos.DataSource = a
    '        ddlProyectos.DataValueField = "idProyecto"
    '        ddlProyectos.DataTextField = "CodigoProyecto"
    '        ddlProyectos.DataBind()


    '        Dim b = From i In bd.Web_Parametros
    '                Where (i.Categoria = "visitas")
    '                Select i.NombreOpcion, i.ValorOpcion

    '        ddvisita.DataSource = b
    '        ddvisita.DataValueField = "NombreOpcion"
    '        ddvisita.DataTextField = "NombreOpcion"
    '        ddvisita.DataBind()
    '    End Using
    'End Sub

    Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
       cargardatos()
    End Sub
    'Sub cargardatos()
    '    Using bd As New bdReportesDataContext
    '        grdResultado.DataSource = bd.rptCategoriazacion(Context.User.Identity.Name, ddvisita.SelectedValue, ddlProyectos.SelectedValue)
    '        grdResultado.DataBind()
    '    End Using
    'End Sub
    'Sub cargardatosExcel()
    '    Using bd As New bdReportesDataContext
    '        grdExportarExcel.DataSource = bd.rptCategoriazacion(Context.User.Identity.Name, ddvisita.SelectedValue, ddlProyectos.SelectedValue)
    '        grdExportarExcel.DataBind()
    '    End Using
    'End Sub
    Protected Sub btnExpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnExpt.Click

    End Sub

    Protected Sub exportarAExcel()


        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page

        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)

        'form.Controls.Add(tabla)
        form.Controls.Add(Me.grdExportarExcel)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Reporte.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    'Protected Sub btnExp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExp.Click
    '    'cargardatosExcel()
    '    exportarAExcel()
    'End Sub
    'Sub generarGrafica()
    '    Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
    '        cnn.Open()

    '        Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("rptCategoriazacionXProyecto", cnn)
    '            cmd.CommandType = CommandType.StoredProcedure
    '            cmd.Parameters.AddWithValue("@idproyecto", CInt(ddlProyectos.SelectedValue))

    '            Dim reader As IDataReader = cmd.ExecuteReader()



    '            Chart1.DataSource = reader
    '            Chart1.ImageType = DataVisualization.Charting.ChartImageType.Png
    '            With Chart1.Series("Default")
    '                .XValueMember = "Identificador"
    '                .YValueMembers = "li"

    '                .ChartType = SeriesChartType.Radar
    '                .LabelFormat = "P2"
    '                .Item("RadarDrawingStyle") = "Area"
    '                .Item("AreaDrawingStyle") = "Polygon"
    '                .Item("CircularLabelsStyle") = "Horizontal"

    '            End With


    '            With Chart1.Series("v1")
    '                .XValueMember = "Identificador"
    '                .YValueMembers = "v1"

    '                .ChartType = SeriesChartType.Radar
    '                .LabelFormat = "P2"
    '                .Item("RadarDrawingStyle") = "Area"
    '                .Item("AreaDrawingStyle") = "Polygon"
    '                .Item("CircularLabelsStyle") = "Horizontal"

    '            End With

    '            With Chart1.Series("v2")
    '                .XValueMember = "Identificador"
    '                .YValueMembers = "v2"

    '                .ChartType = SeriesChartType.Radar
    '                .LabelFormat = "P2"
    '                .Item("RadarDrawingStyle") = "Area"
    '                .Item("AreaDrawingStyle") = "Polygon"
    '                .Item("CircularLabelsStyle") = "Horizontal"
    '            End With

    '            With Chart1.Series("v3")
    '                .XValueMember = "Identificador"
    '                .YValueMembers = "v3"

    '                .ChartType = SeriesChartType.Radar
    '                .LabelFormat = "P2"
    '                .Item("RadarDrawingStyle") = "Area"
    '                .Item("AreaDrawingStyle") = "Polygon"
    '                .Item("CircularLabelsStyle") = "Horizontal"
    '            End With
    '            'Chart1.Series("Default").XValueMember = "Identificador"
    '            'Chart1.Series("Default").YValueMembers = "Porcentaje"
    '            Chart1.DataBind()
    '            'Chart1.Series("Default").ChartType = SeriesChartType.Radar
    '            ''Chart1.Series("Default").ChartType = SeriesChartType.Line
    '            'Chart1.Series("Default").LabelFormat = "P2"
    '            'Chart1.Series("Default")("RadarDrawingStyle") = "Area"
    '            'Chart1.Series("Default")("AreaDrawingStyle") = "Polygon"
    '            'Chart1.Series("Default")("CircularLabelsStyle") = "Horizontal"



    '            Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "P0"
    '            Chart1.ChartAreas("ChartArea1").AxisY.Maximum = 1
    '            Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = False




    '        End Using

    '        cnn.Close()

    '    End Using

    'End Sub

End Class
